package com.oracle.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;

import com.oracle.entity.po.StorePO;
import com.oracle.entity.po.WarehousePO;

import com.oracle.service.warehouse.WarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/Warehouse")
public class WarehouseController {

    @Autowired
    private WarehouseService warehouseService;


    @RequestMapping(value = "getPage", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getPage(@RequestParam("limit") int limit, @RequestParam("curr") int curr, HttpServletRequest request, HttpServletResponse response) {
        Page<WarehousePO> page = new Page<>(curr, limit);
        List list =  warehouseService.selectPage(page, null).getRecords();
        Integer count = warehouseService.selectCount(null);
        Map<String, Object> jsonmap = new HashMap<String, Object>();
        jsonmap.put("code", 0);
        jsonmap.put("data", list);
        jsonmap.put("count", count);
        jsonmap.put("msg", "查询成功");
        return jsonmap;
    }

    /**
     * [返回员工业绩信息]
     *
     * @author lwf
     * @date 2019/12/28
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @version
     */
    @RequestMapping(value = "getStore", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getStore( HttpServletRequest request, HttpServletResponse response) {
        List list =  warehouseService.getStore();
        Map<String, Object> jsonmap = new HashMap<String, Object>();
        jsonmap.put("code", 0);
        jsonmap.put("data", list);
        jsonmap.put("msg", "查询成功");
        return jsonmap;
    }

    /**
     * [返回员工业绩信息详情]
     *
     * @param id 员工编号
     * @param request
     * @param response
     * @author lwf
     * @date 2019/12/28
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @version
     */
    @RequestMapping(value = "getStoreDetail", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getStoreDetail(@RequestParam("id") int id, HttpServletRequest request, HttpServletResponse response) {
        List list = warehouseService.getStoreDetail(id);
        Map<String, Object> jsonmap = new HashMap<String, Object>();
        jsonmap.put("code", 0);
        jsonmap.put("data", list);
        jsonmap.put("msg", "查询成功");
        return jsonmap;
    }

    @RequestMapping(value = "insertStore", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> insertStore(@RequestBody String data, HttpServletRequest request, HttpServletResponse response) {
        JSONObject jsonObject = JSON.parseObject(data);
        StorePO storePO = new StorePO();
        storePO.setGoodsId(jsonObject.getInteger("goodsId"));
        storePO.setWarehouseId(jsonObject.getInteger("warehouseId"));
        storePO.setNum(jsonObject.getInteger("num"));
        warehouseService.insertStore(storePO);
        Map<String, Object> jsonmap = new HashMap<String, Object>();
        jsonmap.put("msg", "添加成功");
        return jsonmap;
    }

    @RequestMapping(value = "updateStore", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> updateStore(@RequestBody String data, HttpServletRequest request, HttpServletResponse response) {
        JSONObject jsonObject = JSON.parseObject(data);
        StorePO storePO = new StorePO();
        storePO.setGoodsId(jsonObject.getInteger("goodsId"));
        storePO.setWarehouseId(jsonObject.getInteger("warehouseId"));
        storePO.setNum(jsonObject.getInteger("num"));
        warehouseService.updateStore(storePO);
        Map<String, Object> jsonmap = new HashMap<String, Object>();
        jsonmap.put("msg", "修改成功");
        return jsonmap;
    }




}